<?php

/*
	info: 用户组管理
	date: 2011-11-20
*/

if (!defined('ADMIN_WEBINROOT'))
{
	header("HTTP/1.0 404 Not Found");
	exit();
}

$webglobal['file_usergroup'] = array();

//用户组列表
if ($webglobal['mode']=='usergroup') {
	$webglobal['file_usergroup']['dataarray'] = array();
	$query = $db->query("SELECT * FROM {$webconfig['dbpre']}user_group");
	$temptr = 1;

	while ($tempData = $db->FetchArray($query)) {
		$tempData['tdclass'] = (($temptr++)%2==0) ? ' class="evenrow"' : '';
		$webglobal['file_usergroup']['dataarray'][$tempData['id']] = $tempData;
	}

//添加用户组
} elseif ($webglobal['mode']=='usergroupadd') {
	if (global_CheckSubmit('adminsubmit')) {
		$webrequest['name'] = isset($webrequest['name']) ? trim($webrequest['name']) : 0;
		$webrequest['purview'] = global_Intval($webrequest['purview']);
		$strPurview = $webrequest['purview'] ? implode(',', $webrequest['purview']) : '';
		if ($webrequest['name'] == '') {
			admin_ShowMessage('用户组名称不能为空！', '', 'BACK');
		} else {
			admin_CheckFromForm(ADMIN_WEBURLPATH."/index.php?mode=usergroup");
			$db->Query("INSERT INTO {$webconfig['dbpre']}user_group(name, purview) VALUES('{$webrequest['name']}', '{$strPurview}')");
			$tempnewsid = $db->InsertId();

			admin_AddLog("添加用户组|id: {$tempnewsid}; name: {$webrequest['name']}");
			admin_ShowMessage('用户组添加成功。', ADMIN_WEBURLPATH."/index.php?mode=usergroup");
		}
	}
	else {
		$webglobal['file_usergroup']['purviewlist'] = global_getPurviewListData();
		admin_StartFromForm();
	}
//修改用户组
} elseif ($webglobal['mode']=='usergroupedit') {
	$webrequest['editid'] = isset($webrequest['editid']) ? intval($webrequest['editid']) : 0;
	if ($webrequest['editid'] <= 0)
	{
		admin_ShowMessage('不存在该用户组。', '', 'BACK');
	}
	$query = $db->Query("SELECT * FROM {$webconfig['dbpre']}user_group WHERE id='{$webrequest['editid']}'");
	if (!$webglobal['file_usergroup']['editinfo'] = $db->FetchArray($query)) {
		admin_ShowMessage('不存在该用户组。', '', 'BACK');
	}

	if (global_CheckSubmit('adminsubmit'))
	{
		$webrequest['name'] = isset($webrequest['name']) ? trim($webrequest['name']) : 0;
		$webrequest['purview'] = global_Intval($webrequest['purview']);
		$strPurview = $webrequest['purview'] ? implode(',', $webrequest['purview']) : '';

		if ($webrequest['name'] == '') {
			admin_ShowMessage('用户组名称不能为空！', '', 'BACK');
		} else {
			admin_CheckFromForm(ADMIN_WEBURLPATH."/index.php?mode=usergroup");

			$db->Query("UPDATE {$webconfig['dbpre']}user_group SET name='{$webrequest['name']}',purview='{$strPurview}' WHERE id='{$webrequest['editid']}'");

			admin_AddLog("修改用户组| id: {$webrequest['editid']}; title: {$webrequest['name']}");
			admin_ShowMessage('用户组修改成功。', ADMIN_WEBURLPATH."/index.php?mode=usergroup");
		}
	}
	else
	{
		$webglobal['file_usergroup']['purviewlist'] = global_getPurviewListData($webglobal['file_usergroup']['editinfo']['purview']);

		admin_StartFromForm();
	}

} elseif ($webglobal['mode']=='usergroupdel') {
	$webrequest['delid'] = isset($webrequest['delid']) ? intval($webrequest['delid']) : 0;
	if ($webrequest['delid'] <= 0)
	{
		admin_ShowMessage('不存在该用户组。', '', 'BACK');
	}
	$query = $db->Query("SELECT id, name FROM {$webconfig['dbpre']}user_group WHERE id='{$webrequest['delid']}'");
	if (!$tempData = $db->FetchArray($query)) {
		admin_ShowMessage('不存在该用户组。', '', 'BACK');
	}
	$db->Query("DELETE FROM {$webconfig['dbpre']}user_group WHERE id = '{$webrequest['delid']}'");

	admin_AddLog("删除用户组| id: {$webrequest['delid']}; title: {$tempData['name']}");
	admin_ShowMessage('用户组删除成功。', ADMIN_WEBURLPATH."/index.php?mode=usergroup");
}

template_Display('usergroup', 1);

?>